package wh.bos.web.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;


import wh.bos.domain.base.Courier;
import wh.bos.service.base.CourierService;
import wh.bos.web.action.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier>{

	@Autowired
	private CourierService courierService;

	
	//根据条件查询取派员信息
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Specification<Courier> specification = new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("CourierNum").as(String.class)
							,model.getCourierNum() );
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("Company").as(String.class)
							,"%"+model.getCompany()+"%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p3 = cb.equal(root.get("Type").as(String.class)
							, model.getType());
					list.add(p3);
				}
				Join<Object, Object> standardRoot = root.join("standard",JoinType.INNER);
				if (model.getStandard() != null && StringUtils.isNotBlank(model.getStandard().getName())) {
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class)
							, "%"+model.getStandard().getName()+"%");
				}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		
		//调用业务层,查询数据
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Courier> pageDate = courierService.findPageDate(specification,pageable);
		
		//返回客户端数据
		pushPageDateToValueStack(pageDate);
		return SUCCESS;
		
	}
	
	//添加或者修改业务员管理
		@Action(value="courier_save",results={@Result(name="success",type="redirect",
				location="./pages/base/courier.html")})
		public String courier_save(){
			courierService.save(model);
			return SUCCESS;
		}
		private String ids;
		public void setIds(String ids) {
			this.ids = ids;
		}
		
	//作废业务员
		@Action(value="courier_delBatch",results={@Result(name="success",type="redirect"
				,location="./pages/base/courier.html")})
		public String delBatch(){
			String[] idArray = ids.split(",");
			courierService.delBatch(idArray);
			return SUCCESS;
			
		}
	//还原业务员
		@Action(value="courier_restore",results={@Result(name="success",type="redirect"
				,location="./pages/base/courier.html")})
		public String restore(){
			String[] idArray = ids.split(",");
			courierService.restore(idArray);
			return SUCCESS;
		}
		
		
		
		
		
		
}
